package com.transfer.entity.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.staff.entity.Staff;
import com.transfer.entity.Expatriate;

public class ExpatriateQueryDTO {
	private Long id;
	private String expaDepartment;//外派部门
	private String expaPosition;//外派职位
	private Date expaStarDate;//外派开始时间
	private Date expaEndDate;//外派结束时间
	private String reason;//外派原因
	private String phoneNum;//联系电话
	private String staffName;//
	private String staffNum;//
	
	private Long staffId;

	public ExpatriateQueryDTO(Expatriate expatriate,Staff staff) {
		super();
		this.id = expatriate.getId();
		this.expaDepartment = expatriate.getExpaDepartment();
		this.expaPosition = expatriate.getExpaPosition();
		this.expaStarDate = expatriate.getExpaStarDate();
		this.expaEndDate = expatriate.getExpaEndDate();
		this.reason = expatriate.getReason();
		this.phoneNum = expatriate.getPhoneNum();
		this.staffName = expatriate.getStaffName();
		this.staffNum = expatriate.getStaffNum();
		this.staffId = staff.getId();
	}
	public ExpatriateQueryDTO() {
		super();
	}
	public Long getId() {
		return id;
	}
	public String getExpaDepartment() {
		return expaDepartment;
	}
	public String getExpaPosition() {
		return expaPosition;
	}
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getExpaStarDate() {
		return expaStarDate;
	}
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getExpaEndDate() {
		return expaEndDate;
	}

	public String getReason() {
		return reason;
	}
	public String getPhoneNum() {
		return phoneNum;
	}
	public String getStaffName() {
		return staffName;
	}
	public String getStaffNum() {
		return staffNum;
	}
	public Long getStaffId() {
		return staffId;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public void setExpaDepartment(String expaDepartment) {
		this.expaDepartment = expaDepartment;
	}
	public void setExpaPosition(String expaPosition) {
		this.expaPosition = expaPosition;
	}
	public void setExpaStarDate(Date expaStarDate) {
		this.expaStarDate = expaStarDate;
	}
	public void setExpaEndDate(Date expaEndDate) {
		this.expaEndDate = expaEndDate;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}
	public void setPhoneNum(String phoneNum) {
		this.phoneNum = phoneNum;
	}
	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}
	public void setStaffNum(String staffNum) {
		this.staffNum = staffNum;
	}
	public void setStaffId(Long staffId) {
		this.staffId = staffId;
	}
	
	//提供static的工具方法：根据当前 userQueryDTO 对象来组装动态查询条件
	public static Specification<Expatriate> getSpecification(ExpatriateQueryDTO userQueryDTO)
	{
		Specification<Expatriate> spec = new Specification<Expatriate>() {
			public Predicate toPredicate(Root<Expatriate> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != userQueryDTO && !StringUtils.isEmpty(userQueryDTO.getStaffName())) {
					 Predicate  p1 =  cb.like(root.get("staffName").as(String.class),"%"+ userQueryDTO.getStaffName() + "%");
					 list.add(p1);
				 }
				 if(null != userQueryDTO && !StringUtils.isEmpty(userQueryDTO.getStaffNum())) {
					 Predicate  p2 =  cb.like(root.get("staffNum").as(String.class),"%"+ userQueryDTO.getStaffNum() + "%");
					 list.add(p2);
				 }
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}	
}
